package com.wang.myapplication.fragment;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wang.myapplication.R;
import com.wang.myapplication.activity.MyHttpback;
import com.wang.myapplication.base.BaseFragment;
import com.wang.myapplication.base.RecyclerBaseAdapter;
import com.wang.myapplication.network.HttpRequest;
import com.wang.myapplication.network.bean.KMInfoListResponse;
import com.wang.myapplication.network.bean.KMInfoListResponse.Kmlist;

import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * create an instance of this fragment.
 */
public class KnowFragment extends BaseFragment {

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    private RecyclerBaseAdapter<Kmlist> recyclerBaseAdapter;

    @Override
    public int bindLayout() {
        return R.layout.fragment_know;
    }

    @Override
    public void initView(View view) {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                reqKMInfoList(true);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                reqKMInfoList(false);
            }
        });

        refreshLayout.autoRefresh();


        recyclerBaseAdapter = new RecyclerBaseAdapter<Kmlist>(R.layout.lviewitem_kminfolist) {
            @Override
            public void handItemView(MyBaseViewHolder holder, Kmlist pKmlist, final int pPosition) {

                ImageView imgIcon = holder.findViewById(R.id.imgIcon);

                Glide.with(mContext).load(pKmlist.viewpath).into(imgIcon);
                //imgIcon.setOnClickListener(new View.OnClickListener() {
                //    @Override
                //    public void onClick(View v) {
                //        Toast.makeText(MyApp.getAppContext(), "啊哈哈" + pPosition, Toast.LENGTH_SHORT).show();
                //    }
                //});


            }
        };


        //recyclerView使用
        //1、layoutManager
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(layoutManager);
        //2、间隔线
        mRecyclerView.addItemDecoration(new DividerItemDecoration(mContext, layoutManager.getOrientation()));
        //3、setAdapter
        mRecyclerView.setAdapter(recyclerBaseAdapter);

        recyclerBaseAdapter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Object object = v.getTag();
                Toast.makeText(mActivity, "点击了" + v.getTag()+v.getId(), Toast.LENGTH_SHORT).show();
            }
        });
    }


    @Override
    public void doBusiness(Context mContext) {
    }

    @Override
    public void widgetClick(View v) {

    }


    private void reqKMInfoList(final boolean isRefresh) {
        if (isRefresh)
            HttpRequest.reqKMInfoList(new MyHttpback<KMInfoListResponse>() {
                @Override
                public void onSuccessBack(KMInfoListResponse obj) {
                    List<Kmlist> dataLists = obj.transferDatalists(obj);
                    recyclerBaseAdapter.replaceDataList(dataLists);
                    completeRefresh(isRefresh);
                }
            });


    }

    private void completeRefresh(boolean isRefresh) {
        if (isRefresh)
            refreshLayout.finishRefresh();
        else
            refreshLayout.finishLoadmore();
    }

}
